import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/page/home/convenient/order/order_processing_report_controller.dart';
import 'package:kkhousekeep/widget/bottom_confirm_bar.dart';
import 'package:kkhousekeep/widget/form_widget.dart';

import '../../../../widget/custom_app_bar.dart';
import '../../../../widget/custom_multi_picker.dart';
import '../../../../widget/custom_picker.dart';
import '../../../../widget/custom_table.dart';

class OrderProcessingReportPage extends StatelessWidget {
  const OrderProcessingReportPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
      child: GetBuilder<OrderProcessingReportController>(
          init: OrderProcessingReportController(),
          builder: (controller) => Scaffold(
              backgroundColor: const Color(0xFFf4f5f6),
              appBar: CustomAppBar("处理上报"),
              body: SafeArea(
                left: false,
                right: false,
                child: Column(
                  children: [
                    Expanded(
                      child: Obx(
                        () => Container(
                          margin: EdgeInsets.only(top: 16.h, bottom: 16.h),
                          color: Colors.white,
                          child: SingleChildScrollView(
                            child: Column(
                              mainAxisSize: MainAxisSize.min,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                _buildDetail(context, controller),
                                Container(
                                    color: const Color(0xFFf4f5f6),
                                    height: 2.h),
                                CustomTable.buildFormTitle("处理结果",
                                    isRequired: true),
                                TextField(
                                  maxLength: 50,
                                  controller:
                                      controller.resultsTextEditController,
                                  decoration: InputDecoration(
                                      hintText: '请输入处理结果',
                                      contentPadding:
                                          const EdgeInsets.symmetric(
                                              horizontal: 0, vertical: 0),
                                      isDense: true,
                                      counterStyle: TextStyle(
                                          color: (controller
                                                      .results.value.length ==
                                                  50)
                                              ? const Color(0xFFDD5045)
                                              : const Color(0xFFCECECE),
                                          fontSize: 32.sp),
                                      border: InputBorder.none,
                                      hintStyle: TextStyle(
                                          color: const Color(0xFFCECECE),
                                          fontSize: 32.sp)),
                                ).marginOnly(
                                    left: 32.w, right: 32.w, bottom: 30.h),
                                Container(
                                    color: const Color(0xFFf4f5f6),
                                    height: 2.h),
                                CustomTable.buildFormTitle("原因分析"),
                                TextField(
                                  maxLength: 50,
                                  controller: controller
                                      .reasonAnalysisTextEditController,
                                  decoration: InputDecoration(
                                      hintText: '请输入原因分析',
                                      contentPadding:
                                          const EdgeInsets.symmetric(
                                              horizontal: 0, vertical: 0),
                                      isDense: true,
                                      counterStyle: TextStyle(
                                          color: (controller.reasonAnalysis
                                                      .value.length ==
                                                  50)
                                              ? const Color(0xFFDD5045)
                                              : const Color(0xFFCECECE),
                                          fontSize: 32.sp),
                                      border: InputBorder.none,
                                      hintStyle: TextStyle(
                                          color: const Color(0xFFCECECE),
                                          fontSize: 32.sp)),
                                ).marginOnly(
                                    left: 32.w, right: 32.w, bottom: 30.h),
                                DecoratedBox(
                                  decoration: BoxDecoration(
                                      border: Border.all(
                                          color: Colors.red, width: 1.0)),
                                ),
                                Container(
                                    color: const Color(0xFFf4f5f6),
                                    height: 2.h),
                                CustomTable.buildFormTitle("预防措施"),
                                TextField(
                                  controller:
                                      controller.precautionTextEditController,
                                  maxLength: 50,
                                  decoration: InputDecoration(
                                      hintText: '请输入使用建议',
                                      contentPadding:
                                          const EdgeInsets.symmetric(
                                              horizontal: 0, vertical: 0),
                                      isDense: true,
                                      counterStyle: TextStyle(
                                          color: (controller.precaution.value
                                                      .length ==
                                                  50)
                                              ? const Color(0xFFDD5045)
                                              : const Color(0xFFCECECE),
                                          fontSize: 32.sp),
                                      border: InputBorder.none,
                                      hintStyle: TextStyle(
                                          color: const Color(0xFFCECECE),
                                          fontSize: 32.sp)),
                                ).marginOnly(
                                    left: 32.w, right: 32.w, bottom: 30.h),
                                Container(
                                    color: const Color(0xFFf4f5f6),
                                    height: 2.h),
                                CustomTable.buildFormTitle("维修评价"),
                                TextField(
                                  controller:
                                      controller.evaluationTextEditController,
                                  maxLength: 50,
                                  decoration: InputDecoration(
                                    hintText: '请输入维修评价议',
                                    contentPadding: const EdgeInsets.symmetric(
                                        horizontal: 0, vertical: 0),
                                    isDense: true,
                                    counterStyle: TextStyle(
                                        color: (controller
                                                    .evaluation.value.length ==
                                                50)
                                            ? const Color(0xFFDD5045)
                                            : const Color(0xFFCECECE),
                                        fontSize: 32.sp),
                                    border: InputBorder.none,
                                    hintStyle: TextStyle(
                                        color: const Color(0xFFCECECE),
                                        fontSize: 32.sp),
                                  ),
                                ).marginOnly(
                                    left: 32.w, right: 32.w, bottom: 30.h),
                                Container(
                                    color: const Color(0xFFf4f5f6),
                                    height: 2.h),
                                CustomTable.buildFormTitle("维修照片",
                                    isRequired: true),
                                buildDescriptionPicture(),
                                Container(
                                    color: const Color(0xFFf4f5f6),
                                    height: 2.h),
                                Padding(
                                  padding: EdgeInsets.only(left: 32.w),
                                  child: FormWidget.edit(
                                    labelText: "维修费用",
                                    hintText: "请输入维修费用",
                                    textEditingController:
                                        controller.costTextEditController,
                                    keyboardType: TextInputType.number,
                                  ),
                                ),
                                Container(
                                    color: const Color(0xFFf4f5f6),
                                    height: 2.h),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                    CustomTable.buildFormTitle("协助人员")
                                        .marginOnly(right: 60.w),
                                    Expanded(
                                      child: CustomTable.buildSelection(
                                        text: controller.staff.value,
                                        onTap: () {
                                          controller.getStaffList().then(
                                                (value) => _showStaffPicker(),
                                              );
                                        },
                                      ),
                                    ),
                                    SizedBox(
                                      width: 32.w,
                                    ),
                                  ],
                                )
                              ],
                            ),
                          ),
                        ),
                      ),
                    ),
                    buildBottomSheet(),
                  ],
                ),
              ))),
    );
  }

  Widget _buildDetail(
      BuildContext buildContext, OrderProcessingReportController controller) {
    OrderProcessingReportController controller = Get.find();
    return CustomTable(
      children: [
        TableRow(children: [
          CustomTable.buildFormTitle("故障类型"),
          CustomTable.buildFormContent(controller.fatalType.value)
              .marginOnly(left: 60.w),
        ]),
        TableRow(children: [
          CustomTable.buildFormTitle("维修标准", isRequired: true),
          Padding(
            padding: EdgeInsets.only(left: 60.w, right: 32.w),
            child: CustomTable.buildSelection(
              text: controller.standard.value,
              onTap: () {
                controller.getStandardList().then(
                      (value) => _showStandardPicker((index) => {
                            controller
                                .selectStandard(controller.standards[index])
                          }),
                    );
              },
            ),
          )
        ]),
      ],
    );
  }

  buildDescriptionPicture() {
    OrderProcessingReportController controller = Get.find();
    return Container(
      height: 198.w,
      margin: EdgeInsets.only(left: 32.w, bottom: 30.h),
      child: ListView.builder(
        scrollDirection: Axis.horizontal,
        itemExtent: 210.w,
        itemCount: controller.imageDescList.length,
        itemBuilder: (BuildContext context, int index) {
          return Container(
            padding: EdgeInsets.only(right: 12.w),
            width: 192.w,
            height: 198.w,
            child: Stack(
              children: [
                Positioned(
                  left: 0,
                  bottom: 0,
                  child: InkWell(
                    onTap: () => controller
                        .performClickImageItem(controller.imageDescList[index]),
                    child: Container(
                      width: 180.w,
                      height: 180.w,
                      padding: EdgeInsets.all(
                          controller.imageDescList[index].isPlaceholder
                              ? 0
                              : 6.w),
                      decoration: controller.imageDescList[index].isPlaceholder
                          ? null
                          : BoxDecoration(
                              borderRadius: BorderRadius.circular(12.w),
                              border: Border.all(
                                color: const Color(0XFFC6C9CC),
                                width: 2.w,
                                style: BorderStyle.solid,
                              ),
                            ),
                      child: Container(
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(12.w),
                          image: controller.imageDescList[index].isPlaceholder
                              ? DecorationImage(
                                  image: AssetImage(
                                    'assets/images/${controller.imageDescList[index].absolutePath}',
                                  ),
                                  fit: BoxFit.fill)
                              : DecorationImage(
                                  image: FileImage(
                                    File(controller
                                        .imageDescList[index].absolutePath),
                                  ),
                                  fit: BoxFit.fill),
                        ),
                      ),
                    ),
                  ),
                ),
                Positioned(
                  top: 0,
                  right: 0,
                  width: 36.w,
                  height: 36.w,
                  child: Visibility(
                    visible: !controller.imageDescList[index].isPlaceholder,
                    child: InkWell(
                      onTap: () => controller.performClickDeleteImage(
                          controller.imageDescList[index]),
                      child: Image.asset(
                        'assets/images/common_clear.png',
                      ),
                    ),
                  ),
                ),
              ],
            ),
          );
        },
      ),
    );
  }

  buildDescriptionPictureItem() {
    return Container(
        alignment: Alignment.centerLeft,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(6.r)),
          image: const DecorationImage(
              fit: BoxFit.fill,
              image: NetworkImage(
                  "https://img1.doubanio.com/view/photo/l/public/p2623486479.webp")),
        ));
  }

  void _showStaffPicker() {
    OrderProcessingReportController controller = Get.find();
    Get.bottomSheet(
      CustomMultiPick(
        indexs: controller.selectedStaffIndexs,
        pickList: controller.staffsStringList,
        confirmCallBack: (indexs) => {controller.selectStaff(indexs)},
        cancelCallBack: (index) => {Get.back()},
      ),
    );
  }

  void _showStandardPicker(confirmCallback, {String? value}) {
    OrderProcessingReportController controller = Get.find();
    var index = 0;
    if (value != null) {
      index = controller.standardsStringList.indexOf(value);
    }
    Get.bottomSheet(
      CustomPick(
        index: index,
        pickList: controller.standardsStringList,
        confirmCallBack: confirmCallback,
        cancelCallBack: (index) => {Get.back()},
      ),
    );
  }

  buildBottomSheet() {
    OrderProcessingReportController controller = Get.find();
    return BottomConfirmBar(
      leftText: "取消",
      onTapLeft: () {
        Get.back();
      },
      rightText: "提交",
      onTapRight: () {
        controller.summit();
      },
    );
  }
}
